//
//  UIImage+Extension.swift
//  WeiBo
//
//  Created by DDC on 2017/9/15.
//  Copyright © 2017年 DDC. All rights reserved.
//

import Foundation

extension UIImage {
    
    func JCLAvatarImage(image:UIImage, size: CGSize, backColor:UIColor?) -> UIImage? {
        
        //图像上下文  内存中开辟一个地址 跟屏幕无关
        let rect = CGRect(origin: CGPoint(), size: size)
        
        //开启图片上下文
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        
        //背景填充
        backColor?.setFill()
        UIRectFill(rect)
        //画圆角图像
        let path = UIBezierPath(ovalIn: rect)
        path.addClip()
        //开始绘图
        image.draw(in: rect)
        
        //绘制内切圆边框
        UIColor.darkGray.setStroke()
        path.lineWidth = 2//线宽
        path.stroke()//描边
    
        //取得结果
        let result = UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        //返回结果
        return result
    }
    
}
